<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\crm\model\Customer as CustomerModel;
use app\crm\model\StatisticHuiyuanTjModel;
use app\crm\model\TransferShop;
use app\crm\model\CustomerConfig as CustomerConfigModel;
use app\crm\model\CustomerGrade as CustomerGradeModel;
use think\response\Json;
use think\facade\Db;

class StatisticHuiyuanTj extends ApiCommon
{
    protected $customerModel;
    protected $customerConfigModel;
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission' => ['yb_vip_export'],
            'allow' => ['users', 'info']
        ];
        event('CheckAuth',$action);
        $request = $this->request;
        $a = strtolower($request->action());

        $this->customerModel = new CustomerModel();
        $this->customerConfigModel = new CustomerConfigModel();
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 会员统计
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function huiyuan_tongji(CustomerModel $customerModel): Json
    {
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $this->param['user_id'] = $this->userInfo['id'];

        $huiyuanModel = new StatisticHuiyuanTjModel();
        $data = $huiyuanModel->getDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 预备会员顾客列表
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function getYBVipList(CustomerModel $customerModel): Json
    {
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $this->param['user_id'] = $this->userInfo['id'];

        $huiyuanModel = new StatisticHuiyuanTjModel();
        $data = $huiyuanModel->getYBVipList($this->param);
        // echo "<pre>";
        // print_r($data);
        // exit;
        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 预备会员导出
     */
    public function yb_vip_export()
    {
        // $start_time = $_POST['start_time'];
        // $end_time = $_POST['end_time'];

        $start_time = strtotime($_POST['start_time']);
        $end_time = strtotime($_POST['end_time']);

        $where = [];

        array_push($where,['final_consumption','>=',$start_time]);
        array_push($where,['final_consumption','<=',$end_time]);
        array_push($where,['member_classification','in',[71,72,73]]);
        array_push($where,['membership_leve','=',0]);

        $customer_list = Db::name('customer')
        ->where($where)
        ->column('id,customer_name,telephone,shop_id,nurse_id,member_classification,consumption,final_consumption');

        

        $customer_ids = array_values(array_unique(array_column($customer_list,'id')));

        $where = [];

        array_push($where,['customer_id','in',$customer_ids]);

        $scarList = Db::name('customer_scar')->where($where)->column('customer_setting_id_type','customer_id'); 

        $where = [];

        array_push($where,['id','in',$scarList]);

        $scar_names = Db::name('setting')->where($where)->column('name','id'); 

        // echo "<pre>";
        // print_r($scarList);
        // exit;

        $shop_ids = array_values(array_unique(array_column($customer_list,'shop_id')));

		$nurse_id = array_values(array_unique(array_column($customer_list,'nurse_id')));
        
        $where = [];

        array_push($where,['shop_id','in',$shop_ids]);

        $shopList = Db::name('shop')->where($where)->column('shop_name','shop_id');


        $where = [];

        array_push($where,['id','in',$nurse_id]);

        $nurseList = Db::name('shop_person')->where($where)->column('name','id');

        // 性别数组
        $customer_type = [71=>'C类客户',72=>'B类客户',73=>'A类客户'];


        $list = [];

        foreach($customer_list as $key => $val){
            $one = [];

            $one['customer_name'] = $val['customer_name'];

            $one['telephone'] = $val['telephone'];

            $one['shop_name'] = $shopList[$val['shop_id']] ?? '';

            $one['nurse_name'] = $nurseList[$val['nurse_id']] ?? '';

            $one['final_consumption'] = $val['final_consumption'] > 0 ? date('Y-m-d H:i:s',$val['final_consumption']) : '';

            $one['consumption'] = $val['consumption'];

            $one['member_classification'] = $customer_type[$val['member_classification']];

            $scarID = $scarList[$val['id']] ?? '';

            $one['scar_name'] = $scar_names[$scarID] ?? '';
            array_push($list,$one);
            
        }

        // echo "<pre>";
        // print_r($list);

        // return json_encode($list);

        $callName = [

            'customer_name'   =>'顾客姓名',

            'telephone' =>'手机号',

            'shop_name' =>'门店',

            'nurse_name' =>'护理师',

            'scar_name' =>'疤痕类型',

            'member_classification' =>'客户分类',

            'consumption'   =>'累计消费金额',

            'final_consumption'   =>'最新消费时间',


        ];

        $downUrl = exportExcelData('预备会员导出',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>"; 
    }

}